<?php
/**
 * XGPHP 轻量级PHP框架
 * @link http://xgphp.xg3.cn
 * @version 2.0.0
 * @license http://www.apache.org/licenses/LICENSE-2.0
 * @author 讯高科技 <xungaokeji@qq.com>
*/
namespace xg;
use \xg\facade\Route;
use \xg\facade\Cookie;
use \xg\facade\Config;
use \xg\facade\Request;
use \helper\File;
use \helper\Arr;

class Lang {
	public $config=[
		// 默认语言
		'default' => 'zh-cn',
		// Cookie标记
		'cookie' => 'xg_lang',
		// 请求参数标记
		'request' => 'lang',
		// 语言映射
		'mapping' => [
			'zh-hans-cn' => 'zh-cn',
		],
	];
	public $lang='zh-cn';
	public $data=[];
	public function __construct(){
		$this->config=Config::get('lang',$this->config);
		$files=File::findFiles(File::path('xg:php','lang'));
		if($app=Route::app()){
			$appFiles=File::findFiles(File::path('xg:apps',$app,'lang'));
			$files=Arr::extend($files,$appFiles);
		}
		foreach($files as $file){
			$lang=substr(basename($file),0,-4);
			$datai=include $file;
			$this->data[$lang]=Arr::extend($this->data[$lang],$datai);
		}
		$this->lang=$this->detect();
	}
	//设置语言数据
	public function setData($data,$lang=null){
		if(!$lang){
			$lang=$this->lang;
		}
		$this->data[$lang]=Arr::merge($this->data[$lang],$data);
		return $this;
	}
	//获取语言数据
	public function getData($lang=null){
		if(!$lang){
			$lang=$this->lang;
		}
		if($this->data[$lang]){
			return $this->data[$lang];
		}
		return [];
	}
	//将文本转换未指定语言
	public function get($info,$vars=[],$lang=null){
		if(!$lang){
			$lang=$this->lang;
		}
		$result=Arr::get($this->data[$lang],$info);
		if(!$result){
			$result=$info;
		}
		$result=preg_replace_callback('/\{\:([a-zA-Z0-9_\-]+)\}/', function($matches) use ($vars) {
			return isset($vars[$matches[1]]) ? $vars[$matches[1]] : $matches[0];
		}, $result);
		return $result;
	}
    public function load($files,$lang){
        if (!isset($this->data[$lang])) {
            $this->data[$lang] = [];
        }
        $lang = [];

        foreach ((array) $files as $file) {
            if (is_file($file)) {
                // $result = $this->parse($file);
                // $lang   = array_change_key_case($result) + $lang;
            }
        }

        if (!empty($lang)) {
            $this->data[$lang] = $lang + $this->data[$lang];
        }

        return $this->data[$lang];
    }
	//保存当前语言到浏览器Cookie
	public function cookie($lang){
		if($this->config['cookie']){
			Request::cookie($this->config['cookie'],$lang);
		}
		return $this;
	}
	//侦测并返回当前语言
	public function detect(){
		$mapping=$this->config['mapping'];//映射配置
		$data=$this->data;

		//检测语言是否存在的函数
		$check=function($lang) use ($mapping,$data){
			if($mapping[$lang] and $data[$mapping[$lang]]){
				return $mapping[$lang];
			}
			if($data[$lang]){
				return $lang;
			}
		};

		//请求参数中设置了语言变量
		if($this->config['request']){
			$lang=strtolower(Request::input('get.'.$this->config['request']));
			if($checked=$check($lang)){
				return $checked;
			}
		}
		
		//Cookie中设置了语言变量
		if($this->config['cookie']){
			$cookie=strtolower(Cookie::get($this->config['cookie']));
			if($checked=$check($cookie)){
				return $checked;
			}
		}
		
		//浏览器语言
		$accept=Request::input('server.HTTP_ACCEPT_LANGUAGE');
		if($accept){
			preg_match('/^([a-z\d\-]+)/i', $accept, $matches);
			$accept = strtolower($matches[1]);
			if($checked=$check($accept)){
				return $checked;
			}
		}

		//默认语言
		return $this->lang;
	}
}
?>